package operations;

import MyThrowable.IDRepeatWrongful;
import MyThrowable.InputWrongful;
import student.Student;
import student.StudentList;

import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2022-12-14
 * Time: 7:47
 */
public class DelOperation implements IOperation{
    static String PatternStr2 = "^[0-9]+$";
    @Override
    public void work(StudentList studentList) {

        System.out.println("删除操作!");

        while (true){
            if(studentList.isEmpty()){
                System.out.println("学生已经被清空!");
               break;
            }
            int choice = menu();
            int flag = delt(studentList,choice);
            if(flag == 0){
                break;
            }
        }
    }


    private int delt(StudentList studentList, int choice) {
        switch (choice){
            case 1:
                while (true){
                    System.out.println("删除学生的学号(只能为0~9的数字)：");
                    Scanner scan = new Scanner(System.in);
                    String id = scan.nextLine();
                    try {
                        if (!Pattern.matches(PatternStr2, id)) {
                            throw new InputWrongful("输入有误,请再次输入!");
                        }else {
                            int index = -1;
                            int currentSize = studentList.getUsedsize();
                            for (int i = 0;i < currentSize;i++){
                                Student student = studentList.getPos(i);
                                if(Integer.valueOf(id).equals(student.getId())){
                                    index = i;
                                    break;
                                }
                            }
                            if(index == -1){
                                System.out.println("没有该学生");
                                break;
                            }
                            studentList.remveStudent(index);
                            studentList.setUsedsize(currentSize-1);
                            System.out.println("删除成功");
                            break;
                        }
                    } catch (InputWrongful e) {
                        e.printStackTrace();
                    }
                }
                return 1;
            case 2:
                studentList.Myclear();
                System.out.println("清空成功!");
                return 0;
            case 0:
                return 0;
            default:
                System.out.println("输入有误,请重新输入!");
                return 1;
        }
    }


    private int menu(){
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("*******************************");
            System.out.println("*******请选择删除的类型(0~2)******");
            System.out.println("********** 1.按学号删 ***********");
            System.out.println("*********  2.清空 **************");
            System.out.println("******** 0.返回上一级 ***********");
            System.out.println("*******************************");
            String choice = scan.nextLine();
            if (Pattern.matches(PatternStr2, choice)) {
                return Integer.valueOf(choice);
            }else {
                System.out.println("输入有误!");
            }
        }
   }
}
